import $ from "jquery"



//添加线
function addPolyLine(id, name, type, positions, width, color) {
  viewer.entities.add({
    id: id,
    name: name,
    type: type,
    show: true,
    polyline: {
      positions: Cesium.Cartesian3.fromDegreesArray(positions), //[经度，纬度，经度，纬度...]
      // positions: Cesium.Cartesian3.fromDegreesArrayHeights(positions),
      width: width,
      material: Cesium.Color.fromCssColorString(color),
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
      clampToGround: true
      //arcType: Cesium.ArcType.GEODESIC
    },
  });
}

//添加多边形面
function addPolygon(id, type, positions, color) {
  viewer.entities.add({
    id: id,
    type: type,
    show: true,
    polygon: {
      //heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      hierarchy: Cesium.Cartesian3.fromDegreesArray(positions),
      material: Cesium.Color.fromCssColorString(color).withAlpha(0.5),
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
    }
  });
}

//添加多边形面带黑色边框线
function addPolygonLine(id, type, positions, color) {
  viewer.entities.add({
    id: id,
    type: type,
    show: true,
    polygon: {
      //heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      hierarchy: Cesium.Cartesian3.fromDegreesArray(positions),
      material: Cesium.Color.fromCssColorString(color).withAlpha(0.5),
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
    },
    polyline: {
      positions: Cesium.Cartesian3.fromDegreesArray(positions),
      width: 1,
      material: Cesium.Color.BLACK,
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
      clampToGround: true
    }
  });
}

//添加多边形面带黑色边框线并添加标签
function addPolygonLineLabel(id, type, data, color, alpha) {
  var font = "18px Microsoft YaHei";
  // alpha = alpha | 0.5;
  viewer.entities.add({
    id: id,
    name: data.Name,
    type: type,
    show: true,
    polygon: {
      //heightReference: Cesium.HeightReference.CLAMP_TO_GROUND,
      hierarchy: Cesium.Cartesian3.fromDegreesArray(data.positions),
      material: Cesium.Color.fromCssColorString(color).withAlpha(alpha),
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
    },
    polyline: {
      positions: Cesium.Cartesian3.fromDegreesArray(data.positions),
      width: 1,
      material: Cesium.Color.BLACK,
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
      clampToGround: true
    },
    position: Cesium.Cartesian3.fromDegrees(data.centrepoint.X, data.centrepoint.Y, 10),
    label: {
      text: data.Name,
      font: font,
      fillColor: Cesium.Color.BLACK,
      verticalOrigin: Cesium.VerticalOrigin.BOTTOM,
      // horizontalOrigin: Cesium.HorizontalOrigin.LEFT,
      // horizontalOrigin: Cesium.HorizontalOrigin.CENTER,
      distanceDisplayCondition: new Cesium.DistanceDisplayCondition(0.0, 200000.0),
      show: true
    }
  });
  // viewer.zoomTo(viewer.entities);
  viewer.flyTo(viewer.entities);
}


var ajaxJson = function (url, functionCall) {
  $.ajax({
    //type: "post",
    //dataType: "json",
    url: url,
    cache: false,
    async: true,
    //contentType: 'application/json',
    success: function (json) {
      if (json != null) {
        json = $.parseJSON(json);
        functionCall(json);
      }
      //console.log(data);
      //var datasource = Cesium.GeoJsonDataSource.load(data);
      //viewer.dataSources.add(datasource);
    },
    error: function (data) {
      console.log(data);
    }
  });
}

function addPolygonArea(id, type, data, color, alpha) {
  addPolygonLineLabel(id, type, data, color, alpha);
  // dataArea.push({ id: id, type: type, name: data.Name, positions: data.positions, centrepoint: data.centrepoint, color: color });
}

function getColor(type) {
  let color = '';
  switch (type) {
    case '公共交通场站用地':
      color = '#808080';
      break;
    case '社会公共停车场':
      color = '#808080';
      break;
    case '医疗卫生用地':
      color = 'DeepPink';
      break;
    case '娱乐康体设施用地':
      color = 'FireBrick';
      break;
    case '防护绿地':
      color = 'Green';
      break;
    case '公园绿地':
      color = 'Lime';
      break;
    case '体育用地':
      color = 'LimeGreen';
      break;
    case '社会福利设施用地':
      color = 'MediumVioletRed';
      break;
    case '未知2':
      color = 'Peru';
      break;
    case '商业用地':
      color = 'red';
      break;
    case '未知1':
      color = 'Salmon';
      break;
    case '文化设施用地':
      color = 'SandyBrown';
      break;
    case '供水用地':
      color = 'SteelBlue';
      break;
    case '环卫设施用地':
      color = 'SteelBlue';
      break;
    case '高中用地':
      color = 'Violet';
      break;
    case '小学用地':
      color = 'Violet';
      break;
    case '中学用地':
      color = 'Violet';
      break;
    case '服务设施用地':
      color = 'Wheat';
      break;
    case '居住用地':
      color = 'Yellow';
      break;
    default:
      color = '#ffffff';
      break;
  }
  return color;
}
//获取点位Json数据
function getDataJson(url, type, pointType) {
  //var urlP = "http://180.76.118.107:64970/arcgis/rest/services/waterFacility/MapServer/5/query?where=0%3D0&text=&objectIds=&time=&geometry=&geometryType=esriGeometryEnvelope&inSR=&spatialRel=esriSpatialRelIntersects&relationParam=&outFields=&returnGeometry=true&maxAllowableOffset=&geometryPrecision=&outSR=&returnIdsOnly=false&returnCountOnly=false&orderByFields=&groupByFieldsForStatistics=&outStatistics=&returnZ=false&returnM=false&gdbVersion=&returnDistinctValues=false&f=pjson";
  ajaxJson(url, function (jsonData) {
    if (jsonData != null) {
      var data = json(jsonData, pointType);
      if (pointType == "point") {
        addBillboard(data, type);
      } else if (pointType == "polygon") {
        var color = "#60bde8";
        var alpha = 1;
        if (type == "DL_side") {
          color = "#696969";
          alpha = 0.1;
        } else if (type == "DLM") {
          color = "#A9A9A9";
          alpha = 0.5;
        } else if (type == "fenqu") {
          alpha = 0.1;
        }
        for (var i = 0; i < data.length; i++) {
          addPolygonArea(null, type, data[i], type == "fenqu" ? getColor(data[i].type) : color, alpha);
        }
      } else if (pointType == "line") {
        var color = "";
        var width = 1;
        if (type == "Raindrainageroute") { //雨水排江路线
          color = "rgb(77, 59, 0)";
          width = 3;
        } else if (type == "DikeLine") { //线状堤防
          color = "red";
          width = 5;
        } else if (type == "GangquLine") { //港渠
          color = "green";
          width = 2;
        }
        for (var i = 0; i < data.length; i++) {
          addPolyLine("", type, data[i].positions, width, color);
        }
      }
    }
  });
}

function json(jsonData, pointType) {
  var data = [];
  var length = jsonData.features.length;
  if (pointType == "point") {
    for (var i = 0; i < length; i++) {

      data.push({
        Name: jsonData.features[i].attributes.name ? jsonData.features[i].attributes.name : jsonData.features[i].attributes.STNM,
        X: jsonData.features[i].geometry.x,
        Y: jsonData.features[i].geometry.y,
        //Z: json["waterFacility:Z"]
      });
    }
    //jsonData.features.each(function (i) {
    //    data.push({
    //        Name: json["waterFacility:STNM"] ? json["waterFacility:STNM"] : json["waterFacility:name"],
    //        X: json["waterFacility:longitude"],
    //        Y: json["waterFacility:latitude"],
    //        Z: json["waterFacility:Z"]
    //    });
    //});
  } else if (pointType == "polygon") {
    for (var i = 0; i < length; i++) {
      var ringsLength = jsonData.features[i].geometry.rings.length;
      for (var k = 0; k < ringsLength; k++) {
        var positions = [],
          position = [];
        var X = 0,
          Y = 0;
        var arr = jsonData.features[i].geometry.rings[k];
        for (var j = 0; j < arr.length; j++) {
          arr[j].splice(2, 1);
          positions = positions.concat(arr[j]);
          X += arr[j][0];
          Y += arr[j][1];
        }
        position = {
          X: X / arr.length,
          Y: Y / arr.length
        }
        data.push({
          // Name: jsonData.features[i].attributes.name ? jsonData.features[i].attributes.name : jsonData.features[i].attributes.名称,
          type: jsonData.features[i].attributes.TYPE,
          positions: positions,
          centrepoint: position
        });
      }
    }
  } else if (pointType == "line") {
    for (var i = 0; i < length; i++) {
      var positions = [];
      var arr = jsonData.features[i].geometry.paths[0];
      for (var j = 0; j < arr.length; j++) {
        arr[j].splice(2, 1);
        positions = positions.concat(arr[j]);
      }
      data.push({
        positions: positions
      });
    }
  }
  return data;
}

function loadDL() {
  var url = this.$ipConfig.ip + this.$ipConfig.arcgisPort+ "/arcgis/rest/services/testservers/GY/MapServer/0/query?where=0%3D0&outSR=4326&f=pjson";
  getDataJson(url, "DL_side", "polygon");
}

function loadDLM() {
  var url = this.$ipConfig.ip + this.$ipConfig.arcgisPort+ "/arcgis/rest/services/testservers/GY/MapServer/1/query?where=0%3D0&outSR=4326&f=pjson";
  getDataJson(url, "DLM", "polygon");
}

function loadFQ() {
  var url = this.$ipConfig.ip + this.$ipConfig.arcgisPort+ "/arcgis/rest/services/testservers/GY/MapServer/2/query?where=0%3D0&outSR=4326&f=pjson";
  getDataJson(url, "fenqu", "polygon");
}



export default {
  loadDL,
  loadDLM,
  loadFQ,
}
